package database.connector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;



import engine.Statics;


/*
 * The mysql connector.
 * This contains 
 * 
 * Openeing, getting and closing a connection
 * You should
 * 
 * init
 * do X querys with it,
 * then close.
 * 
 */

public class MySqlConnector implements DBConnector{

	public MySqlConnector(){};	
	private Logger log = Logger.getLogger(MySqlConnector.class);	
	private Connection connection= null;
	
	
	/*
	 * 
	 */
	public Connection getConnection()
	{
		if (connection == null) initConnection();
		return this.connection;
	}
	
	/*
	 * 
	 */
	public void closeConnetion()
	{
		log.info("Trying to close connection...");
		try {
			connection.close();
		} catch (SQLException e) {
			log.warn("Cannot close database..");
		}
	}
	
	/*
	 * 
	 */
	public boolean initConnection()
	{
		log.info("Trying to init connection...");		
		try{
			this.connection = DriverManager.getConnection(Statics.DB_URL, Statics.DB_USER, Statics.DB_PASS);
		}
		catch(SQLException e)
		{
			log.warn("ooops cannot get database access");
			return false;
		}		
		if (this.connection != null) return true;
		return false;
	}
	
}
